{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 检测内存泄漏\n",
    "\n",
    "- **工具**：\n",
    "  - AddressSanitizer (ASan)：编译期插桩，精确定位越界/泄漏。\n",
    "  - LeakSanitizer、Valgrind（Memcheck）、Dr. Memory：运行时检测。\n",
    "  - Windows CRT `_CrtDumpMemoryLeaks`、Visual Studio Diagnostics。\n",
    "- 在 CI 中启用 sanitizer，可提前发现问题。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  ODR（One Definition Rule）\n",
    "\n",
    "- 每个实体（函数/类/变量）在程序中只能有一个一致定义。\n",
    "- 违反会导致链接冲突、未定义行为（同名函数实现不一致、跨翻译单元 inline 函数不一致等）。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ABI 兼容性\n",
    "\n",
    "- ABI 决定二进制布局、调用约定。共享库更新时必须保持 ABI 兼容，否则旧程序链接新库会崩溃。\n",
    "- C ABI 更稳定（函数名不重整、布局简单），C++ ABI 复杂（name mangling、虚表、模板实例等），不同编译器可能不兼容。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 优化级别对调试的影响\n",
    "\n",
    "- `-O2/-O3` 会内联、删除变量、重排指令，导致断点跳跃、局部变量不可见。\n",
    "- 缓解：使用 `-Og`（GCC/Clang）、在关键模块关闭优化、依赖 `-fno-omit-frame-pointer`、借助调试日志或 sanitizers。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# `static_assert`\n",
    "\n",
    "- 编译期断言；条件为 `false` 时报错并显示定制消息。\n",
    "- 场景：模板约束、类型尺寸、接口契约。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "cpp"
    }
   },
   "outputs": [],
   "source": [
    "static_assert(sizeof(long) >= 4, \"long must be at least 32 bits\");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 清晰指出“期望 vs 实际”，给出修复建议："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "cpp"
    }
   },
   "outputs": [],
   "source": [
    "static_assert(std::is_move_constructible_v<T>,\n",
    "  \"Foo<T> requires T to be move constructible; consider wrapping in std::unique_ptr\");"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "name": "C++17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
